/******************************************************************************
 *  Copyright 2013 Bernhard Grünewaldt                                        *
 *                                                                            *
 *  Licensed under the Apache License, Version 2.0 (the "License");           *
 *  you may not use this file except in compliance with the License.          *
 *  You may obtain a copy of the License at                                   *
 *                                                                            *
 *      http://www.apache.org/licenses/LICENSE-2.0                            *
 *                                                                            *
 *  Unless required by applicable law or agreed to in writing, software       *
 *  distributed under the License is distributed on an "AS IS" BASIS,         *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
 *  See the License for the specific language governing permissions and       *
 *  limitations under the License.                                            *
 ******************************************************************************/
package com.google.code.greenwood.confluencemavenreleaseinfomacro.pojo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class ModuleReleaseVersion implements Comparable, Cloneable {
	
	// internal used: artifactId
	private String name;
	
	private Collection<ArtifactInformation> dependencies;

	private Collection<Properties> properties;

	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Collection<ArtifactInformation> getDependencies() {
		return dependencies;
	}

	public void setDependencies(Collection<ArtifactInformation> dependencies) {
		this.dependencies = dependencies;
	}

	public Collection<Properties> getProperties() {
		return properties;
	}

	public void setProperties(Collection<Properties> properties) {
		this.properties = properties;
	}
	
	public ArtifactInformation getDependencyByGroupIdArtifactIdPackaging(String groupId, String artifactId, String packaging) {
		for (ArtifactInformation ainfo : dependencies) {
			if (ainfo != null && ainfo.getArtifactid().equals(artifactId) && ainfo.getGroupid().equals(groupId) && ainfo.getPackaging().equals(packaging)) {
				return ainfo;
			}
		}
		return null;
	}
	
	public Properties getPropertyByPropertyName(String propertyName) {
		for (Properties prop : properties) {
			if (prop != null && prop.getPropertyName().equals(propertyName)) {
				return prop;
			}
		}
		return null;
	}
		


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/**
	 * ONLY COMPARE NAME for getUnionOfModuleReleaseVersionLists()
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ModuleReleaseVersion other = (ModuleReleaseVersion) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	/**
	 * equals Method of ModuleReleaseVersion must only compare name!
	 */
	public static ImmutableSet<ModuleReleaseVersion> getUnionOfModuleReleaseVersionLists(Collection<ModuleReleaseVersion> releaseInformationOneModules, Collection<ModuleReleaseVersion> releaseInformationTwoModules) {
	    // Union based on module.getName()
	    Set<ModuleReleaseVersion> firstModuleSet = ImmutableSet.copyOf(releaseInformationOneModules);
	    Set<ModuleReleaseVersion> secondModuleSet = ImmutableSet.copyOf(releaseInformationTwoModules);
	
	    SetView<ModuleReleaseVersion> modulesUnionView = Sets.union(firstModuleSet, secondModuleSet);
	    ImmutableSet<ModuleReleaseVersion> modulesUnionSet = modulesUnionView.immutableCopy();
	    
	    return modulesUnionSet;
	}
	
	
	public static String foo() {
		return "bar";
	}

	@Override
	public int compareTo(Object o) {
		ModuleReleaseVersion other = (ModuleReleaseVersion) o;
		if (name.equals(other.getName())) {
			return 0;
		}
		return 1;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		ModuleReleaseVersion clone = new ModuleReleaseVersion();
		clone.setName(this.getName());
		Collection<ArtifactInformation> dependenciesClone = null;
		Collection<Properties> propertiesClone = null;
		if (this.getDependencies() != null) {
			dependenciesClone = new ArrayList<ArtifactInformation>();
			for (ArtifactInformation ainfo : this.getDependencies()) {
				dependenciesClone.add((ArtifactInformation)ainfo.clone());
			}
			clone.setDependencies(dependenciesClone);
		}
		if (this.getProperties() != null) {
			propertiesClone = new ArrayList<Properties>();
			for (Properties prop : this.getProperties()) {
				propertiesClone.add((Properties)prop.clone());
			}
			clone.setProperties(propertiesClone);
		}
		return clone;
	}
	
	
	
	
	
}
